

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.dao.LogDao;
import com.yz.dao.ProjectDao;
import com.yz.service.CameraService;
import com.yz.service.CheckService;
import com.yz.service.LogService;
import com.yz.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("logService")
public class LogServiceImpl extends ServiceImpl<LogDao,LogEntity> implements LogService {
    @Autowired
    CheckService checkService;
    @Autowired
    ProjectService projectService;
    @Autowired
    CameraService cameraService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postMe(LogForm data,UserEntity user) {
        LogEntity t = data.getLogEntity();
        long id = 0;
        CheckEntity check = new CheckEntity();
        check.setCreateAt(t.getUpdateAt());
        check.setCreateUser(t.getCreateUser());
        check.setName(projectService.selectById(t.getProjectId()).getName());
        if(t.getType()==1){
            check.setNameForm(contant.type_name[0]);
        }else{
            check.setNameForm(contant.type_name[1]);
        }
        check.setProjectId(t.getProjectId());
        check.setStatus(1);//1未审核，2已完成，3审核失败
        check.setType(1);
        check.setTypeForm(t.getType());
        if(t.getId()==0){
            this.baseMapper.insert(t);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[0]+t.getType()+id);
            checkService.insert(check);
        }else{
            id = t.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[0]+t.getType()+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[0]+t.getType()+id));
            this.baseMapper.update(t,new EntityWrapper<LogEntity>().eq("id",id));
        }
        //图片放入数据库
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",200));
        if(data.getSketch()!=null&& data.getSketch().size()>0){//草图
            for (int i = 0; i <data.getSketch().size() ; i++) {
                CameraEntity item = data.getSketch().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(t.getType());
                item.setTypeForm(200);
                item.setDataId(id);
                item.setFiledName("2");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[13]+id);
                cameraService.insert(item);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteMe(long id) {
        this.baseMapper.deleteById(id);
        checkService.deleteMe(1,id);
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",200));
    }

    @Override
    public LogForm getDetails(long id) {
        LogForm data = new LogForm();
        LogEntity t = new LogEntity();
        t.setId(id);
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",200));
        data.setSketch(cc1s);
        data.setLogEntity(this.baseMapper.selectById(t));
        return data;
    }
}
